package com.lms.logisticsmanagementsystem.controller;

import com.lms.logisticsmanagementsystem.model.Staff;
import com.lms.logisticsmanagementsystem.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private StaffService staffService;

    @GetMapping("/toStaff")
    public String toAddStaff() {
        return "staff/addStaff";
    }

    @GetMapping("/staffInfo")
    public String toStaffInfo() {
        return "staff/staffInfo";
    }

    @GetMapping("/toEditStaffInfo")
    public String toEditStaff(Model model,HttpServletRequest request){
        String id = request.getParameter("id");
        Staff staff = staffService.getStaffInfoById(id);
        model.addAttribute("staff",staff);
        return "staff/editStaff";
    }

    @PostMapping("/addStaff")
    @ResponseBody
    public String addStaff(@RequestBody Staff staff) {
        //向数据库中添加数据
        boolean flag = staffService.addStaff(staff);
        if (flag) {
            return "success";
        } else {
            return "fail";
        }
    }

    @PostMapping("/getStaffInfo")
    @ResponseBody
    public Map<String, Object> getStaffInfo(HttpServletRequest request) {
        //获取表格分页条件
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        //获取后台数据
        List<Staff> staffList = staffService.getStaffInfo();
        List<Staff> curPageInfo = new ArrayList<>();
        Integer curPage = Integer.valueOf(page);
        Integer limitRows = Integer.valueOf(limit);
        //当前行数据索引
        Integer curIndex = (curPage - 1) * limitRows;
        //目标行数据索引
        Integer targetIndex = curIndex + limitRows;
        //数据总数
        Integer totalInfoSize = staffList.size();
        //如果目标行索引大于数据总数，则返回数据总数进行判断
        if (targetIndex > totalInfoSize) {
            targetIndex = totalInfoSize;
        }
        for (int i = curIndex; i < targetIndex; i++) {
            curPageInfo.add(staffList.get(i));
        }
        //设置为layui表格接收格式
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", staffList.size());
        map.put("data", curPageInfo);
        return map;
    }

    @PostMapping("/editStaffInfo")
    @ResponseBody
    public String editStaffInfo(@RequestBody Staff staff){
        System.out.println(staff);
        String str = "";
        //前台数据传输确定
        if (staff==null){
            str="info-error";
            return str;
        }
        //更新数据库信息
        boolean flag = staffService.updateStaffInfoById(staff);
        if (flag){
            str="success";
        }else {
            str="fail";
        }
        return str;
    }

    @PostMapping("/delStaffInfo")
    @ResponseBody
    public String delStaffInfoById(@RequestParam("id")String id){
        if (id==null||id==""){
            return "infoError";
        }
        boolean flag = staffService.delStaffInfoById(Integer.valueOf(id));
        if (flag==true){
            return "success";
        }else {
            return "fail";
        }
    }
}
